package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.*;
import cn.gson.crm.model.mapper.CheckMapper;
import cn.gson.crm.model.mapper.SalesMapper;
import cn.gson.crm.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CheckService {
    @Autowired
    CheckMapper checkMapper;
    @Autowired
    CheckDao checkDao;
    @Autowired
    SellDao sellDao;
    @Autowired
    BusinessDao businessDao;
    @Autowired
    ContractDao contractDao;
    @Autowired
    OfferDao offerDao;
    @Autowired
    Order2Dao order2Dao;
    @Autowired
    OrderDao orderDao;
    @Autowired
    ReturngoodsDao returngoodsDao;
    @Autowired
    ExchangeGoodsDao exchangeGoodsDao;
    @Autowired
    ExchangeGoods2Dao exchangeGoods2Dao;
    @Autowired
    SalesMapper salesMapper;
    //查询所有
    public List<Check> selectCheck(String type,String leix){
        return checkMapper.selectCheck(type,leix);
    }
    //点击传值过来进行查询
    public Map<String,Object> chuan(Integer checkId){
        //查询我的审核表的东西
        System.out.println("coming");
        Check check = checkMapper.select1(checkId);
        //获取审批类型
        String checkType = check.getCheckType();
        Map<String,Object> map = new HashMap<>();
        if (checkType.equals("销售费用")){
            System.out.println("sxdwcds"+checkId);
            List<Check> checks = checkMapper.selectSell(checkId);
            map.put("sell",checks);
        }
        if(checkType.equals("出差申请")){
            System.out.println("sxdwcds2"+checkId);
            List<Check> checks2 =checkMapper.selectBusiness(checkId);
            map.put("business",checks2);
        }
        if(checkType.equals("合同审批")){
            System.out.println("sxdwcds3"+checkId);
            List<Check> checks3=checkMapper.selectContract(checkId);
            map.put("contract",checks3);
        }
        if(checkType.equals("报价审批")){
            System.out.println("sxdwcds4"+checkId);
            List<Check> checks4=checkMapper.selectoffer(checkId);
            map.put("offer",checks4);
        }
        if(checkType.equals("订单审批")){
            System.out.println("sxdwcds5"+checkId);
            List<Check> checks5=checkMapper.selectoder(checkId);
            map.put("order",checks5);
        }
        if(checkType.equals("退货审批")){
            System.out.println("sxdwcds6"+checkId);
            List<Check> checks6=checkMapper.selectgoods(checkId);
            map.put("goods",checks6);
        }
        if(checkType.equals("换货审批")){
            System.out.println("sxdwcds7"+checkId);
            List<Check> checks7=checkMapper.selectgood2(checkId);
            map.put("good2",checks7);
        }
        return  map;
    }
    //修改方法1
    public void updateCheck3(Integer checkId,String checkSpDate,String checkCondition,Long checkName,String checkType) {
        Check check = checkMapper.select1(checkId);
        check.setCheckName(checkName);
        System.err.println("222" + checkName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(checkSpDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        check.setCheckspDate(date);
        check.setCheckCondition(checkCondition);
        checkMapper.update1(checkCondition,checkSpDate,checkId);
        System.err.println("类型"+checkType);
        if(checkType.equals("销售费用")){
            //调用查询方法（销售费用）
            List<Check> checks = checkMapper.selectSell(checkId);
            int checktypeid = checks.get(0).getChecktypeid();
            Sell sell = sellDao.findById(checktypeid).get();
            if(checkCondition.equals("同意")){
                sell.setSellReimburse("已审批");
            }else {
                sell.setSellReimburse("已作废");
            }
            sellDao.save(sell);
            System.err.println("id:" + checkId);
        }
        if(checkType.equals("出差申请")){
            //调用查询方法(出差申请)
            List<Check> checks1 = checkMapper.selectBusiness(checkId);
            int checktypeid2 = checks1.get(0).getChecktypeid();
            Business business = businessDao.findById(checktypeid2).get();
            if(checkCondition.equals("同意")){
                business.setBusinessAppstate("已审批");
                business.setBusinessState("待出差");
                try{
                    long currentTime= new Date().getTime();//当前时间
                    long startTime = business.getBusinessStartTime().getTime();//开始时间
                    long endTime_ = business.getBusinessOverTime().getTime();//结束时间
                    if(currentTime > startTime && currentTime < endTime_){
                        business.setBusinessState("已出差");
                    }else if(currentTime > endTime_){
                        business.setBusinessState("已结束出差");
                        business.setBusinessAppstate("已过期");
                    }
                }catch (Exception e){

                }

            }
            else {
                business.setBusinessAppstate("已作废");
            }
            businessDao.save(business);
        }
        if(checkType.equals("合同审批")){
            //调用查询方法（合同审批）
            List<Check> checks2=checkMapper.selectContract(checkId);
            int checktypeid3 =checks2.get(0).getChecktypeid();
            Contract contract = contractDao.findById(checktypeid3).get();
            if(checkCondition.equals("同意")){
                contract.setEcrState("已审核");
            }
            else {
                contract.setEcrState("已作废");
            }
            contractDao.save(contract);
        }
        if(checkType.equals("报价审批")){
            //调用查询方法(报价审批)
            List<Check> check3=checkMapper.selectoffer(checkId);
            int checktypeid4=check3.get(0).getChecktypeid();
            Offer offer =offerDao.findById((long)checktypeid4).get();
//            System.err.println("销售机会："+offer.getSales().getSalesId());
////            List<Offer> offers = salesMapper.selectOffer((int) offer.getSales().getSalesId());
////            System.out.println("长度:"+offers.size());
////            if(offers.size()>=0){
////                System.err.println("coming");
////                for (int i = 0; i < offers.size(); i++) {
////                    System.err.println("审核状态1："+offers.get(i).getOfferAudit());
////                    if(offers.get(i).getOfferAudit().equals("已审核")){
////                        System.err.println("审核状态2："+offers.get(i).getOfferAudit());
////                        offers.get(i).setOfferAudit("已作废");
////                        offerDao.save(offers.get(i));
////                    }
////                }
////            }
            if(checkCondition.equals("同意")){
                offer.setOfferAudit("已审核");
            }else {
                offer.setOfferAudit("已作废");
            }
            offerDao.save(offer);
        }
        if(checkType.equals("订单审批")){
            //调用查询方法(订单审批)
            List<Check> check4=checkMapper.selectoder(checkId);
            int checktypeid5=check4.get(0).getChecktypeid();
            Order order =order2Dao.findById((long)checktypeid5).get();
            if(checkCondition.equals("同意")){
                order.setEcrState("已通过");
            }else {
                order.setEcrState("已作废");
            }
            orderDao.save(order);
        }
        if(checkType.equals("退货审批")){
            //调用查询方法(订单审批)
            List<Check> check5=checkMapper.selectgoods(checkId);
            int checktypeid6=check5.get(0).getChecktypeid();
            ReturnGoods returnGoods =returngoodsDao.findById(checktypeid6).get();
            if(checkCondition.equals("同意")){
                returnGoods.setEcrState("已审批");
            }else {
                returnGoods.setEcrState("已作废");
            }
            returngoodsDao.save(returnGoods);
        }
        if(checkType.equals("换货审批")){
            //调用查询方法(订单审批)
            List<Check> check6=checkMapper.selectgood2(checkId);
            int checktypeid7=check6.get(0).getChecktypeid();
            ExchangeGoods exchangeGoods =exchangeGoods2Dao.findById((long)checktypeid7).get();
            if(checkCondition.equals("同意")){
                exchangeGoods.setEcrState("已审批");
            }else {
                exchangeGoods.setEcrState("已作废");
            }
            exchangeGoodsDao.save(exchangeGoods);
        }
    }

}
